package com.action.app_landing;

import java.io.File;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletResponse;


import org.apache.struts2.ServletActionContext;
import org.json.JSONObject;

import com.dataProcess.MapPart;
import com.enetiy.Map;
import com.enetiy.Maprecord;
import com.enetiy.Version;
import com.opensymphony.xwork2.ActionSupport;
import com.service.AreaIpService;
import com.service.MapService;
import com.service.VersionService;
import com.util.DataUtil;
import com.util.IPUtil;
import com.util.Log4gUtil;
/**
 * 版本更新
 * @author hzy
 */
public class VersionAction extends ActionSupport {
	private int userid;
	private String apName;
	private String apMac;
	private String mapCoord;
	
	private String nasid;
	private String ver;
	private int siteAreaid;
	private int vertype;//1:安卓;2:iOS
	private String params;
	private String agencyIp;
	private int appType;
	
	private String ip;
	private String mac;
	private int visitType;
	
	private VersionService versionService;
	private MapService mapService;
	private AreaIpService areaIpService;
	
	
	/**
	 * 
	 * 版本检测  >Nf001
	 */
	public void versionNumber(){
		if(params!=null&&!params.equals("")){
			JSONObject  datajson = DataUtil.dataParser(params);
			if(datajson.has("appType"))
				appType = datajson.getInt("appType");
			if(datajson.has("vertype"))
				vertype = datajson.getInt("vertype");
			if(datajson.has("ver"))
				ver = datajson.getString("ver");
			if(datajson.has("agencyIp"))
				agencyIp = datajson.getString("agencyIp");
		}
		if(appType==0){
			appType=1;
		}
		
		
		Log4gUtil.getLog().info("versionNumber-进入版本更新-:"+params);
		
		HttpServletResponse response = ServletActionContext.getResponse();
		net.sf.json.JSONObject j = DataUtil.sendJson();
		PrintWriter out=null;
		try {
			out = response.getWriter();
				//最新版
				List<Version> newVers = versionService.selectVersion(1,vertype ,appType);
				boolean fignewver = false;
				//可用版
				List<Version> usVers = versionService.selectVersion(2,vertype,appType);
				boolean figusVers = false;
				
				if(!newVers.isEmpty()){
					for (int i = 0; i < newVers.size(); i++) {
						if(newVers.get(i).getVersionNum().equals(ver)){
							j.put("isAudit", newVers.get(i).getIsAudit());
							fignewver = true;
						}
					}
				}
				
				if(fignewver){//提交为最新版
					j.put("code", 1);
					j.put("us", 0);
				}else{
					if(!usVers.isEmpty()){
						for (int i = 0; i < usVers.size(); i++) {
							if(usVers.get(i).getVersionNum().equals(ver)){
								figusVers = true;
							}
						}
					}
				
				String verNameadk ="";
				if(vertype==1){  //安卓需要的adk文件
					String path = IPUtil.getFilePath()+"/ver";
					System.out.println("verpath"+path);
					File f = new File(path);
					String[] list = f.list();
					if(list!=null){
						for (int i = 0; i < list.length; i++) {
							if(list[i].contains("ver"+newVers.get(0).getVersionNum())){
								verNameadk=list[i];
							}
						}
					}
				}
				if(figusVers){
					j.put("code", 1);
					j.put("us",1);
					j.put("ver", newVers.get(0).getVersionNum());
					if(vertype==1){
						if(agencyIp!=null&&!agencyIp.equals(""))
							j.put("pic", agencyIp+"/ver/"+verNameadk);
						else
							j.put("pic", IPUtil.getIp()+"/ver/"+verNameadk);
					}
				}else{
					j.put("code", 1);
					j.put("us", 2);
					j.put("ver",newVers.get(0).getVersionNum());
					if(vertype==1){
						if(agencyIp!=null&&!agencyIp.equals(""))
							j.put("pic", agencyIp+"/ver/"+verNameadk);
						else
							j.put("pic", IPUtil.getIp()+"/ver/"+verNameadk);
				}
			}
		}
		} catch (Exception e) {
			j.put("code", -1);
			j.put("msg", "服务器异常");
			e.printStackTrace();
		}
		Log4gUtil.getLog().info("versionNumber-输出版本更新-:"+j.toString());
		out.print(j);
	}
	
	/**
	 * 提交测试数据
	 */
	public void testDate(){
		if(params!=null&&!params.equals("")){
			JSONObject datajson = DataUtil.dataParser(params);
			if(datajson.has("apName"))
				apName=datajson.getString("apName");
			if(datajson.has("apMac"))
				apMac=datajson.getString("apMac");
			if(datajson.has("userid"))
				userid =datajson.getInt("userid");
			if(datajson.has("nasid"))
				nasid =datajson.getString("nasid");
			if(datajson.has("ip"))
				ip =datajson.getString("ip");
			if(datajson.has("mac"))
				mac =datajson.getString("mac");
			if(datajson.has("siteAreaid")){
				if(siteAreaid==0)
					siteAreaid=datajson.getInt("siteAreaid");
			}
		}
		Log4gUtil.getLog().info("testDate-进入-:"+params);
		HttpServletResponse response = ServletActionContext.getResponse();
		net.sf.json.JSONObject j = DataUtil.sendJson();
		PrintWriter out= null;
		try {
			out = response.getWriter();
			System.out.println("apMac"+apMac+"  nasid"+nasid);
			//apMac>6字节
			if(apMac==null||apMac.length()<6){
				apMac="";	
			}else {
				apMac=apMac.substring(0,apMac.length() - 3);
			}
			//nasid>15字节
			if(nasid==null||nasid.length()<15){
				nasid="";	
			}
			
			if(!apMac.equals("")&&!nasid.equals("")
					&&siteAreaid!=0){
				Map selectMap = mapService.selectMap(apMac, nasid, siteAreaid);
				if(selectMap==null){
					Maprecord maprecord = new Maprecord(userid, mapCoord, apName, apMac, new Date(), siteAreaid,nasid,ip,mac,10);
					mapService.insertMaprecord(maprecord);
					j.put("msg","已添加记录");
				}else{
					j.put("msg","已存在");
				}
				j.put("code", 1);
			}else{
				j.put("code", 0);
				j.put("msg","有提交不规侧数据");
			}
		
		} catch (Exception e) {
			j.put("code", -1);
			e.printStackTrace();
		}
		out.print(j);
		Log4gUtil.getLog().info("testDate-输出-:"+j.toString());
	}
	
	/**
	 * 
	 * 检测接口  >Nf002
	 */
	public void isAlive(){
		if(params!=null&&!params.equals("")){
			JSONObject datajson = DataUtil.dataParser(params);
			if(datajson.has("apName"))
				apName=datajson.getString("apName");
			
			if(datajson.has("mapCoord"))
				mapCoord=datajson.getString("mapCoord");
			if(datajson.has("apMac"))
				apMac=datajson.getString("apMac");
			if(datajson.has("userid"))
				userid =datajson.getInt("userid");
			if(datajson.has("nasid"))
				nasid =datajson.getString("nasid");
			if(datajson.has("ip"))
				ip =datajson.getString("ip");
			if(datajson.has("mac"))
				mac =datajson.getString("mac");
			if(datajson.has("visitType"))
				visitType =datajson.getInt("visitType");
			if(datajson.has("siteAreaid"))
				siteAreaid=datajson.getInt("siteAreaid");
		}
		if(visitType!=2)
			Log4gUtil.getLog().info("isAlive-进入-:"+params);
		HttpServletResponse response = ServletActionContext.getResponse();
		net.sf.json.JSONObject j = DataUtil.sendJson();
		PrintWriter out= null;
		try {
			out = response.getWriter();
			int areaId =siteAreaid;
			int netState = 0;//断protal上临时放行的网络
			if(siteAreaid!=2&&siteAreaid!=1){//深职院,深大没有apmac
				MapPart mapPart = new MapPart();
				areaId = mapPart.getAreaIdAlive(mapService, userid, apName, siteAreaid, mapCoord, apMac, nasid, mac, ip, visitType,areaIpService);
				if((visitType==1)){
					netState = mapPart.cacheOutNet(ip, areaIpService, netState);
				}
			}
			j.put("siteAreaid", areaId);
			j.put("netState", netState);
			j.put("code", 1);
		} catch (Exception e) {
			j.put("code", -1);
			e.printStackTrace();
		}
		out.print(j);
		if(visitType!=2)
			Log4gUtil.getLog().info("isAlive-输出-:"+j.toString());
	}

	
	
	
	public String getVer() {
		return ver;
	}

	public void setVer(String ver) {
		this.ver = ver;
	}


	public void setNasid(String nasid) {
		this.nasid = nasid;
	}

	public void setVersionService(VersionService versionService) {
		this.versionService = versionService;
	}

	public void setVertype(int vertype) {
		this.vertype = vertype;
	}

	public void setSiteAreaid(int siteAreaid) {
		this.siteAreaid = siteAreaid;
	}

	public void setParams(String params) {
		this.params = params;
	}

	public void setAgencyIp(String agencyIp) {
		this.agencyIp = agencyIp;
	}

	public void setMapService(MapService mapService) {
		this.mapService = mapService;
	}

	public void setUserid(int userid) {
		this.userid = userid;
	}

	public void setApName(String apName) {
		this.apName = apName;
	}

	public void setApMac(String apMac) {
		this.apMac = apMac;
	}

	public void setMapCoord(String mapCoord) {
		this.mapCoord = mapCoord;
	}

	public void setAppType(int appType) {
		this.appType = appType;
	}

	public void setIp(String ip) {
		this.ip = ip;
	}

	public void setMac(String mac) {
		this.mac = mac;
	}

	public void setVisitType(int visitType) {
		this.visitType = visitType;
	}

	public void setAreaIpService(AreaIpService areaIpService) {
		this.areaIpService = areaIpService;
	}
	
	
}
